/*
* ============================================================================
* 功能描述 ：串口命令层
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"


static uart_buf_s uart_buf;

static void _data_tx_pack(void)
{
#if 0
	if(uart_buf.recv_hand_flag == true)
	{
		char app_data[10];
		app_data[0] = 0x01;
		app_data[1] = 0x02;
		app_data[2] = 0x03;
		app_data[3] = 0x04;
		app_data[4] = 0x05;
		app_data[5] = 0x06;
		app_data[6] = 0x06;
		app_data[7] = 0x06;
		app_data[8] = 0x06;
		Uart_Tx_buf(app_data, 9);
		uart_buf.recv_hand_flag == false;
	}
#else
	if(uart_buf.recv_hand_flag == true)
	{
		Uart_Tx_buf(uart_buf.recv_buf, uart_buf.recv_len);
		uart_buf.recv_hand_flag = false;
	}


#endif

}


static void _uart_data_handle(void)
{
	char  th;
	char  ch;
	if(uart_buf.recv_buf[0] == 0x55)
	{
		if(uart_buf.recv_buf[1] == 0xaa)
		{
			th = uart_buf.recv_buf[6]*256+uart_buf.recv_buf[7];	// 计算传输命令字节数，判断校验位，数据长度是否足够
			uart_buf.cmd_len = th+9;
			if(uart_buf.recv_len == uart_buf.cmd_len)
			{
				ch = specialSumCheck(uart_buf.recv_buf, uart_buf.recv_len -1);					// 校验数据
				
				Uart_Tx_buf(uart_buf.recv_buf, uart_buf.recv_len);
				if(ch == uart_buf.recv_buf[uart_buf.recv_len -1])
				{
					int i;
					for(i = 0; i < uart_buf.recv_len; i++)
					{
						uart_buf.cmd_buf[i] = uart_buf.recv_buf[i];
					}
					uart_buf.recv_hand_flag = true;
					uart_buf.recv_len = 0;
				}
			}
			
		}
	}
	else if (uart_buf.recv_len >12)
		uart_buf.recv_len = 0;
}

void uart_cmd_rx_cplt_callback(void)
{
	uart_buf.recv_buf[uart_buf.recv_len] = RCREG;
	uart_buf.recv_len++;
	RCIF = 0;
	if (uart_buf.recv_len >12)
	{
		uart_buf.recv_len = 0;
	}
	_uart_data_handle();
}

void uart_cmd_send_buf(unsigned char *buf, int length)
{

	unsigned char i =0 ;
	for(i=0;i<length;i++)
	{
		if(TRMT == 1)			// TRMT为1时，发送寄存器为空
		{
			TXREG =buf[i];

			while(TRMT==0);		// TRMT为0时，数据装填
		}
		
	}

}

void Zigbee_Cmd_handle(void)
{
	if(uart_buf.recv_hand_flag == true)
	{
		
		switch(uart_buf.cmd_buf[4])
		{
			case device_manage:

				break;
			case Network_manage:

				break;
			case Serial_config_manage:

				break;
			case OTA_manage:

				break;
			case Product_manage:

				break;
			default:
				dbg(Dbg, DbgNoPerror, "Unknow msg, not deal...\n\r");
				break;

		}
	}

}

void app_init(void)
{

	_data_tx_pack();

}


